#include <stdio.h>

int g_count=0;
 
int CanPlace(int row,int col,int (*chess)[8])
{
	int i;
	
    for (i=0;i<8;i++) {
        if (chess[i][col]==1) {//check col
            return 0;
        }
        if (chess[row][i]==1) {//check row
            return 0;
        }
    } 
 
    for (i=0;i<8;i++) {//check left-front
        if (row-i<0||col-i<0) {
            break;
        }
        if (chess[row-i][col-i]==1) {
            return 0;
        }
    }    
 
    for(i=0;i<8;i++){//check right-front
        if(row-i<0||col+i>7){
            break;
        }
        if(chess[row-i][col+i]==1){
            return 0;
        } 
    }    
 
    for(i=0;i<8;i++){//check left-below
        if(row+i>7||col-i<0){
            break;
        }
        if(chess[row+i][col-i]==1){
            return 0;
        }
    }    
 
    for(i=0;i<8;i++) {//check right-below
        if(row+i>7||col+i>7){
            break;
        }
        if(chess[row+i][col+i]==1){
            return 0;
        }
    }
    
    return 1;
}
void EightQueen(int row,int col,int (*chess)[8])
{
    int chess2[8][8];
	int i, j;
    
    //put last scene to temp 2Darray
    for(i=0;i<8;i++){
        for(j=0;j<8;j++){
            chess2[i][j]=chess[i][j];
        }
    }
 
    if(row==8){//show result
        for(i=0;i<8;i++){
            for(j=0;j<8;j++){         
				printf("%d ", chess2[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        g_count++;
    }
    else{
        for(j=0;j<8;j++){
            if(CanPlace(row,j,chess2)){
                
                chess2[row][j]=1;//put chess
                
                EightQueen(row+1,j,chess2);//move to next row
                
                chess2[row][j]=0;//remove chess to use for next by same chess 2Darray
                
            }
        } 
    }
}
 
int main()
{
    int chess[8][8]={0};
    
    EightQueen(0,0,chess);
    
	printf("The %d\n", g_count);
    
    return 0;
}
